home *** CD-ROM | disk | FTP | other *** search
/ Freelog 70 / Freelog070.iso / Internet / EasyPHP / easyphp1-8_setup.exe / {app} / phpmyadmin / libraries / tbl_move_copy.php < prev    next >
Encoding:
PHP Script  |  2004-12-28  |  16.7 KB  |  354 lines

  1. <?php
  2. /* $Id: tbl_move_copy.php,v 1.2 2004/12/28 11:11:12 nijel Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Inserts existing entries in a PMA_* table by reading a value from an old entry
  7.  *
  8.  * @param   string  The array index, which Relation feature to check
  9.  *                  ('relwork', 'commwork', ...)
  10.  * @param   string  The array index, which PMA-table to update
  11.  *                  ('bookmark', 'relation', ...)
  12.  * @param   array   Which fields will be SELECT'ed from the old entry
  13.  * @param   array   Which fields will be used for the WHERE query
  14.  *                  (array('FIELDNAME' => 'FIELDVALUE'))
  15.  * @param   array   Which fields will be used as new VALUES. These are the important
  16.  *                  keys which differ from the old entry.
  17.  *                  (array('FIELDNAME' => 'NEW FIELDVALUE'))
  18.  
  19.  * @global  string  relation variable
  20.  *
  21.  * @author          Garvin Hicking <me@supergarv.de>
  22.  */
  23. function PMA_duplicate_table_info($work, $pma_table, $get_fields, $where_fields, $new_fields) {
  24.     global $cfgRelation;
  25.  
  26.     $last_id = -1;
  27.  
  28.     if ($cfgRelation[$work]) {
  29.         $select_parts = array();
  30.         $row_fields = array();
  31.         foreach ($get_fields AS $nr => $get_field) {
  32.             $select_parts[] = PMA_backquote($get_field);
  33.             $row_fields[$get_field] = 'cc';
  34.         }
  35.  
  36.         $where_parts = array();
  37.         foreach ($where_fields AS $_where => $_value) {
  38.             $where_parts[] = PMA_backquote($_where) . ' = \'' . PMA_sqlAddslashes($_value) . '\'';
  39.         }
  40.  
  41.         $new_parts = array();
  42.         $new_value_parts = array();
  43.         foreach ($new_fields AS $_where => $_value) {
  44.             $new_parts[] = PMA_backquote($_where);
  45.             $new_value_parts[] = PMA_sqlAddslashes($_value);
  46.         }
  47.  
  48.         $table_copy_query = 'SELECT ' . implode(', ', $select_parts)
  49.                           . ' FROM ' . PMA_backquote($cfgRelation[$pma_table])
  50.                           . ' WHERE ' . implode(' AND ', $where_parts);
  51.         $table_copy_rs    = PMA_query_as_cu($table_copy_query);
  52.  
  53.         while ($table_copy_row = @PMA_DBI_fetch_assoc($table_copy_rs)) {
  54.             $value_parts = array();
  55.             foreach ($table_copy_row AS $_key => $_val) {
  56.                 if (isset($row_fields[$_key]) && $row_fields[$_key] == 'cc') {
  57.                     $value_parts[] = PMA_sqlAddslashes($_val);
  58.                 }
  59.             }
  60.  
  61.             $new_table_query = 'INSERT IGNORE INTO ' . PMA_backquote($cfgRelation[$pma_table])
  62.                             . ' (' . implode(', ', $select_parts) . ', ' . implode(', ', $new_parts) . ')'
  63.                             . ' VALUES '
  64.                             . ' (\'' . implode('\', \'', $value_parts) . '\', \'' . implode('\', \'', $new_value_parts) . '\')';
  65.  
  66.             $new_table_rs    = PMA_query_as_cu($new_table_query);
  67.             $last_id = PMA_DBI_insert_id();
  68.         } // end while
  69.  
  70.         return $last_id;
  71.     }
  72.  
  73.     return true;
  74. } // end of 'PMA_duplicate_table_info()' function
  75.  
  76.  
  77. /**
  78.  * Copies or renames table
  79.  * FIXME: use RENAME
  80.  *
  81.  * @author          Michal ─îiha┼Ö <michal@cihar.com>
  82.  */
  83. function PMA_table_move_copy($source_db, $source_table, $target_db, $target_table, $what, $move) {
  84.     global $cfgRelation, $dblist, $err_url, $sql_query;
  85.  
  86.     // set export settings we need
  87.     $GLOBALS['use_backquotes'] = 1;
  88.     $GLOBALS['asfile']         = 1;
  89.  
  90.     // Ensure the target is valid
  91.     if (count($dblist) > 0 &&
  92.         (PMA_isInto($source_db, $dblist) == -1 || PMA_isInto($target_db, $dblist) == -1)) {
  93.         exit();
  94.     }
  95.  
  96.     $source = PMA_backquote($source_db) . '.' . PMA_backquote($source_table);
  97.     if (empty($target_db)) $target_db = $source_db;
  98.  
  99.     // This could avoid some problems with replicated databases, when
  100.     // moving table from replicated one to not replicated one
  101.     PMA_DBI_select_db($target_db);
  102.  
  103.     $target = PMA_backquote($target_db) . '.' . PMA_backquote($target_table);
  104.  
  105.     // do not create the table if dataonly
  106.     if ($what != 'dataonly') {
  107.         require_once('./libraries/export/sql.php');
  108.  
  109.         $no_constraints_comments = true;
  110.         $sql_structure = PMA_getTableDef($source_db, $source_table, "\n", $err_url);
  111.         unset($no_constraints_comments);
  112.  
  113.         $parsed_sql =  PMA_SQP_parse($sql_structure);
  114.  
  115.         /* nijel: Find table name in query and replace it */
  116.         $i = 0;
  117.         while ($parsed_sql[$i]['type'] != 'quote_backtick') $i++;
  118.  
  119.         /* no need to PMA_backquote() */
  120.         $parsed_sql[$i]['data'] = $target;
  121.  
  122.         /* Generate query back */
  123.         $sql_structure = PMA_SQP_formatHtml($parsed_sql, 'query_only');
  124.         // If table exists, and 'add drop table' is selected: Drop it!
  125.         $drop_query = '';
  126.         if (isset($GLOBALS['drop_if_exists']) && $GLOBALS['drop_if_exists'] == 'true') {
  127.             $drop_query = 'DROP TABLE IF EXISTS ' . PMA_backquote($target_db) . '.' . PMA_backquote($target_table);
  128.             $result        = PMA_DBI_query($drop_query);
  129.  
  130.             if (isset($sql_query)) {
  131.                 $sql_query .= "\n" . $drop_query . ';';
  132.             } else {
  133.                 $sql_query = $drop_query . ';';
  134.             }
  135.  
  136.             // garvin: If an existing table gets deleted, maintain any entries
  137.             // for the PMA_* tables
  138.             $maintain_relations = TRUE;
  139.         }
  140.  
  141.         $result        = @PMA_DBI_query($sql_structure);
  142.         if (isset($sql_query)) {
  143.             $sql_query .= "\n" . $sql_structure . ';';
  144.         } else {
  145.             $sql_query = $sql_structure . ';';
  146.         }
  147.  
  148.         if (($move || isset($GLOBALS['constraints'])) && isset($GLOBALS['sql_constraints'])) {
  149.             $parsed_sql =  PMA_SQP_parse($GLOBALS['sql_constraints']);
  150.  
  151.             $i = 0;
  152.             while ($parsed_sql[$i]['type'] != 'quote_backtick') $i++;
  153.  
  154.             /* no need to PMA_backquote() */
  155.             $parsed_sql[$i]['data'] = $target;
  156.  
  157.             /* Generate query back */
  158.             $GLOBALS['sql_constraints'] = PMA_SQP_formatHtml($parsed_sql, 'query_only');
  159.             $result          = PMA_DBI_query($GLOBALS['sql_constraints']);
  160.             if (isset($sql_query)) {
  161.                 $sql_query .= "\n" . $GLOBALS['sql_constraints'];
  162.             } else {
  163.                 $sql_query = $GLOBALS['sql_constraints'];
  164.             }
  165.  
  166.             unset($GLOBALS['sql_constraints']);
  167.         }
  168.  
  169.     } else {
  170.         $sql_query='';
  171.     }
  172.  
  173.     // Copy the data
  174.     //if ($result != FALSE && ($what == 'data' || $what == 'dataonly')) {
  175.     if ($what == 'data' || $what == 'dataonly') {
  176.         $sql_insert_data = 'INSERT INTO ' . $target . ' SELECT * FROM ' . $source;
  177.         PMA_DBI_query($sql_insert_data);
  178.         $sql_query      .= "\n\n" . $sql_insert_data . ';';
  179.     }
  180.  
  181.     require_once('./libraries/relation.lib.php');
  182.     $cfgRelation = PMA_getRelationsParam();
  183.  
  184.     // Drops old table if the user has requested to move it
  185.     if ($move) {
  186.  
  187.         // This could avoid some problems with replicated databases, when
  188.         // moving table from replicated one to not replicated one
  189.         PMA_DBI_select_db($source_db);
  190.  
  191.         $sql_drop_table = 'DROP TABLE ' . $source;
  192.         PMA_DBI_query($sql_drop_table);
  193.  
  194.         // garvin: Move old entries from PMA-DBs to new table
  195.         if ($cfgRelation['commwork']) {
  196.             $remove_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
  197.                           . ' SET     table_name = \'' . PMA_sqlAddslashes($target_table) . '\', '
  198.                           . '        db_name    = \'' . PMA_sqlAddslashes($target_db) . '\''
  199.                           . ' WHERE db_name  = \'' . PMA_sqlAddslashes($source_db) . '\''
  200.                           . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
  201.             $rmv_rs    = PMA_query_as_cu($remove_query);
  202.             unset($rmv_query);
  203.         }
  204.  
  205.         // garvin: updating bookmarks is not possible since only a single table is moved,
  206.         // and not the whole DB.
  207.         // if ($cfgRelation['bookmarkwork']) {
  208.         //     $remove_query = 'UPDATE ' . PMA_backquote($cfgRelation['bookmark'])
  209.         //                   . ' SET     dbase = \'' . PMA_sqlAddslashes($target_db) . '\''
  210.         //                   . ' WHERE dbase  = \'' . PMA_sqlAddslashes($source_db) . '\'';
  211.         //     $rmv_rs    = PMA_query_as_cu($remove_query);
  212.         //     unset($rmv_query);
  213.         // }
  214.  
  215.         if ($cfgRelation['displaywork']) {
  216.             $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
  217.                             . ' SET     db_name = \'' . PMA_sqlAddslashes($target_db) . '\', '
  218.                             . '         table_name = \'' . PMA_sqlAddslashes($target_table) . '\''
  219.                             . ' WHERE db_name  = \'' . PMA_sqlAddslashes($source_db) . '\''
  220.                             . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
  221.             $tb_rs    = PMA_query_as_cu($table_query);
  222.             unset($table_query);
  223.             unset($tb_rs);
  224.         }
  225.  
  226.         if ($cfgRelation['relwork']) {
  227.             $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation'])
  228.                             . ' SET     foreign_table = \'' . PMA_sqlAddslashes($target_table) . '\','
  229.                             . '         foreign_db = \'' . PMA_sqlAddslashes($target_db) . '\''
  230.                             . ' WHERE foreign_db  = \'' . PMA_sqlAddslashes($source_db) . '\''
  231.                             . ' AND foreign_table = \'' . PMA_sqlAddslashes($source_table) . '\'';
  232.             $tb_rs    = PMA_query_as_cu($table_query);
  233.             unset($table_query);
  234.             unset($tb_rs);
  235.  
  236.             $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation'])
  237.                             . ' SET     master_table = \'' . PMA_sqlAddslashes($target_table) . '\','
  238.                             . '         master_db = \'' . PMA_sqlAddslashes($target_db) . '\''
  239.                             . ' WHERE master_db  = \'' . PMA_sqlAddslashes($source_db) . '\''
  240.                             . ' AND master_table = \'' . PMA_sqlAddslashes($source_table) . '\'';
  241.             $tb_rs    = PMA_query_as_cu($table_query);
  242.             unset($table_query);
  243.             unset($tb_rs);
  244.         }
  245.  
  246.         // garvin: [TODO] Can't get moving PDFs the right way. The page numbers always
  247.         // get screwed up independently from duplication because the numbers do not
  248.         // seem to be stored on a per-database basis. Would the author of pdf support
  249.         // please have a look at it?
  250.  
  251.         if ($cfgRelation['pdfwork']) {
  252.             $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_coords'])
  253.                             . ' SET     table_name = \'' . PMA_sqlAddslashes($target_table) . '\','
  254.                             . '         db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
  255.                             . ' WHERE db_name  = \'' . PMA_sqlAddslashes($source_db) . '\''
  256.                             . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
  257.             $tb_rs    = PMA_query_as_cu($table_query);
  258.             unset($table_query);
  259.             unset($tb_rs);
  260.             /*
  261.             $pdf_query = 'SELECT pdf_page_number '
  262.                        . ' FROM ' . PMA_backquote($cfgRelation['table_coords'])
  263.                        . ' WHERE db_name  = \'' . PMA_sqlAddslashes($target_db) . '\''
  264.                        . ' AND table_name = \'' . PMA_sqlAddslashes($target_table) . '\'';
  265.             $pdf_rs = PMA_query_as_cu($pdf_query);
  266.  
  267.             while ($pdf_copy_row = PMA_DBI_fetch_assoc($pdf_rs)) {
  268.                 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['pdf_pages'])
  269.                                 . ' SET     db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
  270.                                 . ' WHERE db_name  = \'' . PMA_sqlAddslashes($source_db) . '\''
  271.                                 . ' AND page_nr = \'' . PMA_sqlAddslashes($pdf_copy_row['pdf_page_number']) . '\'';
  272.                 $tb_rs    = PMA_query_as_cu($table_query);
  273.                 unset($table_query);
  274.                 unset($tb_rs);
  275.             }
  276.             */
  277.         }
  278.  
  279.         $sql_query      .= "\n\n" . $sql_drop_table . ';';
  280.     } else {
  281.         // garvin: Create new entries as duplicates from old PMA DBs
  282.         if ($what != 'dataonly' && !isset($maintain_relations)) {
  283.             if ($cfgRelation['commwork']) {
  284.                 // Get all comments and MIME-Types for current table
  285.                 $comments_copy_query = 'SELECT
  286.                                             column_name, ' . PMA_backquote('comment') . ($cfgRelation['mimework'] ? ', mimetype, transformation, transformation_options' : '') . '
  287.                                         FROM ' . PMA_backquote($cfgRelation['column_info']) . '
  288.                                         WHERE
  289.                                             db_name = \'' . PMA_sqlAddslashes($source_db) . '\' AND
  290.                                             table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
  291.                 $comments_copy_rs    = PMA_query_as_cu($comments_copy_query);
  292.  
  293.                 // Write every comment as new copied entry. [MIME]
  294.                 while ($comments_copy_row = PMA_DBI_fetch_assoc($comments_copy_rs)) {
  295.                     $new_comment_query = 'REPLACE INTO ' . PMA_backquote($cfgRelation['column_info'])
  296.                                 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ($cfgRelation['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ') '
  297.                                 . ' VALUES('
  298.                                 . '\'' . PMA_sqlAddslashes($target_db) . '\','
  299.                                 . '\'' . PMA_sqlAddslashes($target_table) . '\','
  300.                                 . '\'' . PMA_sqlAddslashes($comments_copy_row['column_name']) . '\''
  301.                                 . ($cfgRelation['mimework'] ? ',\'' . PMA_sqlAddslashes($comments_copy_row['comment']) . '\','
  302.                                         . '\'' . PMA_sqlAddslashes($comments_copy_row['mimetype']) . '\','
  303.                                         . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation']) . '\','
  304.                                         . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation_options']) . '\'' : '')
  305.                                 . ')';
  306.                     $new_comment_rs    = PMA_query_as_cu($new_comment_query);
  307.                 } // end while
  308.             }
  309.  
  310.             if ($source_db != $target_db) {
  311.                 $get_fields = array('user','label','query');
  312.                 $where_fields = array('dbase' => $source_db);
  313.                 $new_fields = array('dbase' => $target_db);
  314.                 PMA_duplicate_table_info('bookmarkwork', 'bookmark', $get_fields, $where_fields, $new_fields);
  315.             }
  316.  
  317.             $get_fields = array('display_field');
  318.             $where_fields = array('db_name' => $source_db, 'table_name' => $source_table);
  319.             $new_fields = array('db_name' => $target_db, 'table_name' => $target_table);
  320.             PMA_duplicate_table_info('displaywork', 'table_info', $get_fields, $where_fields, $new_fields);
  321.  
  322.             $get_fields = array('master_field', 'foreign_db', 'foreign_table', 'foreign_field');
  323.             $where_fields = array('master_db' => $source_db, 'master_table' => $source_table);
  324.             $new_fields = array('master_db' => $target_db, 'master_table' => $target_table);
  325.             PMA_duplicate_table_info('relwork', 'relation', $get_fields, $where_fields, $new_fields);
  326.  
  327.             $get_fields = array('foreign_field', 'master_db', 'master_table', 'master_field');
  328.             $where_fields = array('foreign_db' => $source_db, 'foreign_table' => $source_table);
  329.             $new_fields = array('foreign_db' => $target_db, 'foreign_table' => $target_table);
  330.             PMA_duplicate_table_info('relwork', 'relation', $get_fields, $where_fields, $new_fields);
  331.  
  332.             // garvin: [TODO] Can't get duplicating PDFs the right way. The page numbers always
  333.             // get screwed up independently from duplication because the numbers do not
  334.             // seem to be stored on a per-database basis. Would the author of pdf support
  335.             // please have a look at it?
  336.             /*
  337.             $get_fields = array('page_descr');
  338.             $where_fields = array('db_name' => $source_db);
  339.             $new_fields = array('db_name' => $target_db);
  340.             $last_id = PMA_duplicate_table_info('pdfwork', 'pdf_pages', $get_fields, $where_fields, $new_fields);
  341.  
  342.             if (isset($last_id) && $last_id >= 0) {
  343.                 $get_fields = array('x', 'y');
  344.                 $where_fields = array('db_name' => $source_db, 'table_name' => $source_table);
  345.                 $new_fields = array('db_name' => $target_db, 'table_name' => $target_table, 'pdf_page_number' => $last_id);
  346.                 PMA_duplicate_table_info('pdfwork', 'table_coords', $get_fields, $where_fields, $new_fields);
  347.             }
  348.             */
  349.         }
  350.     }
  351.  
  352. }
  353. ?>
  354.